package db

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
)

type Config struct {
	Url      string
	DataBase string
}

var Client *mongo.Client
var Database *mongo.Database

func Init(conf Config) (err error) {
	// Set client options
	clientOptions := options.Client().ApplyURI(conf.Url)

	// Connect to MongoDB
	Client, err = mongo.Connect(context.TODO(), clientOptions)
	if err != nil {
		log.Fatal(err)
	}
	// Check the connection
	err = Client.Ping(context.TODO(), nil)

	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Connected to MongoDB!")

	Database = Client.Database(conf.DataBase)
	return
}

func Clone() {
	err := Client.Disconnect(context.TODO())

	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Connection to MongoDB closed.")
}
